﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace _201206QuanLyBanHang.ChucNang
{

    public partial class FormTimKiem : Form
    {
        AutoCompleteStringCollection auto2 = new AutoCompleteStringCollection();       
        DataSet ds = new DataSet();
        SQLCommand tksql = new SQLCommand();
        public string tencuahang = "";
        #region Câu lệnh SQL
        string sql1 = "select tencuahang from khachhang";
        //Tìm kiếm theo khách hàng, tên cửa hàng
        //string TK_sql1 = "select tencuahang,ngaymua, tensp, soluongmua,(gia_sp_ngaymua + gia_chietkhau_ngaymua) as dongia, thanhtien,sotienthanhtoan, ghichu_khmsp" +
        //            "from kh_mua_sp t1, khachhang t2, sanpham t3, khachhang_thanhtoan t4" +
        //            "where t1.makh=t2.makh and t1.masp=t3.masp  and" +
        //            "t1.ngaymua=t4.ngaythanhtoan and t1.ghichu_khmsp <> 'default'" +
        //            "and t1.makh=t4.makh and t2.tencuahang like N'{0}'";
    
        //Tìm kiếm theo ngày tháng

        //Tìm kiếm theo 

        #endregion 
        
       // string sql = "Select dbo.fnChuyenKhongDau(tencuahang) as tencuahang  from khachhang ";
        private ThuVien.Class_Database db = new ThuVien.Class_Database();
         DataConn da=new DataConn();
        public FormTimKiem()
        {
            InitializeComponent();
            LoadDataToCollection();
        }
    
        private void LoadDataToCollection()
        {
            AutoCompleteStringCollection auto2 = new AutoCompleteStringCollection();

            SqlDataReader reader;           
            reader = da.ThucHienReader(sql1);
            if (reader != null)
            {
                while (reader.Read())
                {
                    auto2.Add(reader["tencuahang"].ToString());
                }
            }

            txtTimKiem.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtTimKiem.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtTimKiem.AutoCompleteCustomSource = auto2;        


        }

        private void buttonX_TimKiem_Click(object sender, EventArgs e)
        {
            groupPanel_KQ.Visible = true;   
        }
        //Lấy dữ liệu vào dataGridView
        public void load_dulieu(string tkh)
        {           
            dataGridView_TiKiem.DataSource = tksql.TimKiemKhachHangMuaSanPham(tkh);
            //dataGridView_TiKiem.Columns[0].HeaderText = "sttdd";
            dataGridView_TiKiem.Columns[0].HeaderText = "Tên cửa hàng";
            dataGridView_TiKiem.Columns[1].HeaderText = "Ngày mua";
            dataGridView_TiKiem.Columns[2].HeaderText = "Tên sản phẩm";
            dataGridView_TiKiem.Columns[3].HeaderText = "Số lượng";
            dataGridView_TiKiem.Columns[4].HeaderText = "Đơn giá";
            dataGridView_TiKiem.Columns[5].HeaderText = "Thành tiền";
            dataGridView_TiKiem.Columns[6].HeaderText = "Thanh toán";
            dataGridView_TiKiem.Columns[7].HeaderText = "Ghi chú";
            //sodong = 0;
            //sodong = ds.Tables[0].Rows.Count + 1;         
        }
        // Lấy thông tin khách hàng vào datagridview
        public void load_dulieuTTKH(string tkh)
        {           
            dataGridView_TiKiem.DataSource = tksql.TimKieThongTinKH(tkh);
            dataGridView_TiKiem.Columns[0].HeaderText = "Tên khách hàng";
            dataGridView_TiKiem.Columns[1].HeaderText = "Tên cửa hàng";
            dataGridView_TiKiem.Columns[2].HeaderText = "Địa chỉ";
            dataGridView_TiKiem.Columns[3].HeaderText = "Điện thoại";
           
        }



        #region Tìm kiếm mua bán sản phẩm
        private void expandablePanel2_Click(object sender, EventArgs e)
        {
            load_dulieu(txtTimKiem.Text);
        }
        private void labelX2_Click(object sender, EventArgs e)
        {
            load_dulieu(txtTimKiem.Text);
        }
        #endregion

        #region Tìm kiếm thông tin khách hàng
        private void expandablePanel_ThongTinKH_Click(object sender, EventArgs e)
        {
            load_dulieuTTKH(txtTimKiem.Text);
        }

        private void labelX1_Click(object sender, EventArgs e)
        {
            load_dulieuTTKH(txtTimKiem.Text);
        }
        #endregion

        private void expandablePanel_chiTietDauTu_Click(object sender, EventArgs e)
        {

        }




    }      
}
